package com.zzh.rabbit;

import android.app.Application;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

import com.zzh.lib.bar.java.UltimateBarX;
import com.zzh.lib.core.HLibrary;
import com.zzh.lib.core.utils.HDeviceUtils;
import com.zzh.lib.core.utils.HNetUtils;
import com.zzh.lib.core.utils.HResUtils;
import com.zzh.lib.core.utils.HTaskUtils;
import com.zzh.lib.core.utils.LogUtils;
import com.zzh.lib.core.utils.ToastUtils;
import com.zzh.lib.log.Logger;
import com.zzh.lib.toast.Toaster;
import com.zzh.mvvm.binding.BindingConsumer;
import com.zzh.mvvm.binding.command.BindingCommand;
import com.zzh.mvvm.bus.Messenger;
import com.zzh.mvvm.bus.event.SingleLiveEvent;
import com.zzh.rabbit.demo.base.ds.DataSourceRepo;
import com.zzh.rabbit.demo.base.vm.BaseViewModel;
import com.zzh.rabbit.info.DeviceInfoActivity;
import com.zzh.rabbit.sa.MainActivity;
import com.zzh.rabbit.tb.ToolbarActivity;

import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LifecycleOwner;

/**
 * Created by ZZH on 2021/9/9.
 *
 * @Date: 2021/9/9
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 业务处理逻辑
 */
public class MainViewModel extends BaseViewModel {

    public ObservableField<String> inputData = new ObservableField<>("");
    public ObservableField<Integer> inputVisible = new ObservableField<>(View.VISIBLE);

    public SingleLiveEvent<String> showDeviceInfoText = new SingleLiveEvent<>();

    public BindingCommand onTextChanged = new BindingCommand(new BindingConsumer<Object[]>() {
        @Override
        public void call(Object[] o) {
            if (o != null) {
                Log.e("----", Arrays.toString(o));
            } else {
                Log.e("----", "为空");
            }
        }
    });


    public MainViewModel(@NonNull Application application, DataSourceRepo model) {
        super(application, model);
    }

    public MainViewModel(Application application) {
        super(application);
    }


    public BindingCommand clickNewActivity = new BindingCommand(() -> {

        User user = new User();
        user.name = "张三";
        Messenger.getDefault().send(user);
        Log.e("-----", "---------发出测试消息");

    });
    public BindingCommand clickToolbarCmd = new BindingCommand(() -> {

        startActivity(ToolbarActivity.class);

    });
    public BindingCommand clickIMM1Cmd = new BindingCommand(() -> {

        startActivity(MainActivity.class);

    });
    public BindingCommand clickStartSelectImageActivity = new BindingCommand(() -> {

        startActivity(SelectImageActivity.class);

    });
    public BindingCommand clickPhoneCMD = new BindingCommand(() -> {

        startActivity(DeviceInfoActivity.class);

    });
    public BindingCommand clickToaster = new BindingCommand(() -> {

        ToastUtils.show("测试");

    });
    public BindingCommand clickToasterLong = new BindingCommand(() -> {
        ToastUtils.show("测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试,这是一个测试文字，清楚一甄别");
    });
    public BindingCommand clickToasterDebug = new BindingCommand(() -> {
        ToastUtils.debugShow("测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试,这是一个测试文字，清楚一甄别");
    });

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        super.onDestroy(owner);
        Logger.getInstance().release();
    }

    public void showDeviceInfo() {
        HTaskUtils.getDefaultInstance().execute(() -> {
            LogUtils.e("设备信息");
            StringBuilder builder = new StringBuilder();
            builder.append("Android Version：\t\t").append(Build.VERSION.RELEASE).append(" ").append(Build.VERSION.SDK_INT).append("\n");
            builder.append("设备名称：\t\t").append(HDeviceUtils.getDeviceDevice()).append("\n");
            builder.append("手机型号：\t\t").append(HDeviceUtils.getDeviceModel()).append("\n");
            builder.append("手机(运存)RAM：\t\t").append(HDeviceUtils.getTotalRAM()).append("\n");
            builder.append("手机可用运存：\t\t").append(HDeviceUtils.getAvailableRAM()).append("\n");


            builder.append("屏幕尺寸宽：\t\t").append(HResUtils.getDisplayWidth()).append("\n");
            builder.append("屏幕尺寸高：\t\t").append(HResUtils.getDisplayHeight()).append("\n");

            builder.append("ActionBarSize：\t\t").append(HResUtils.getActionBarHeight()).append("\n");
            builder.append("StatusBarSize：\t\t").append(HResUtils.getStatusBarHeight()).append("\n");
            builder.append("Nav Bar Size：\t\t").append(UltimateBarX.getNavigationBarHeight()).append("\n");
            //builder.append("Nav Bar Size Imm：\t\t").append(ImmersionBar.getNavigationBarHeight(this)).append("\n");

            builder.append("tags：\t\t").append(Build.TAGS).append("\n");
            builder.append("user：\t\t").append(Build.USER).append("\n");
            builder.append("SERIAL：\t\t").append(Build.SERIAL).append("\n");
            builder.append("BRAND：\t\t").append(Build.BRAND).append("\n");
            builder.append("BOARD：\t\t").append(Build.BOARD).append("\n");
            builder.append("CPU：\t\t").append(HDeviceUtils.getCPUAbi()).append("\n");
            try {
                builder.append("IP：\t\t").append(HNetUtils.getIpv4Address()).append("\n");
            } catch (Exception ex) {
                LogUtils.e(ex);

            }

            WebView webView = new WebView(HLibrary.getLastActivity());
            builder.append("WebView UA：\t\t").append(webView.getSettings().getUserAgentString()).append("\n");
            LogUtils.d("这是一个测试用例日志");
            String string = builder.toString();
            LogUtils.d(String.valueOf(string.length()));
            LogUtils.d(string);
            showDeviceInfoText.postValue(string);
        });
    }
}
